﻿using System;
using System.Windows.Input;
using WindowsPhoneMvp.Navigation;

namespace WindowsPhoneMvp.Commands
{
    public static class NavigationCommandExtensions
    {
        private static NavigationCommand CheckICommandType(ICommand type, string actionName)
        {
            if ((type is NavigationCommand) == false)
                throw new ArgumentException(string.Format("{0} can only be used with NavigationCommand", actionName));
            return type as NavigationCommand;
        }
        ///<summary>
        /// The action to execute before navigation
        ///</summary>
        public static ICommand WithPreExecutionAction(this ICommand navigationCommand, Action action)
        {
            var command = CheckICommandType(navigationCommand, "PreExecuteAction");
            command.PreNavigationAction = action;
            return command;
        }
        /// <summary>
        /// Specifies when navigation is allowed
        /// </summary>
        public static ICommand CanExecuteWhen(this ICommand navigationCommand, Func<bool> isTrue)
        {
            var command = CheckICommandType(navigationCommand, "CanExecuteWhen");
            command.InternalCanExecute = isTrue;
            return command;
        }
        /// <summary>
        /// Optional data to pass with the navigation event
        /// </summary>
        public static ICommand WithNavigationData(this ICommand navigationCommand, NavigationData navigationData)
        {
            var command = CheckICommandType(navigationCommand, "WithNavigationData");
            command.NavigationData = navigationData;
            return command;
        }
    }
}
